// cd /projects/data_commons/cw_code/
// qstata cw_geo_czone.do &

di "Started at $S_DATE $S_TIME"

global dir_cw = "/projects/data_commons/cw/"

global ds_raw "${dir_cw}/raw/cw_cty_czone"
global ds_czone "${dir_cw}/cw_geo_fips_czone"

use ${ds_raw}, clear
rename cty_fips fips

local n_obs = _N + 4
local i_row = _N + 1

di "`n_obs'"

set obs `n_obs'

// Revision from David Dorn:
/* REDACTED FOR DISCLOSURE: 
We make changes to certain geographic identifiers (county, czone) 
*/

// Revision by Feng Lin based on NBER
/* REDACTED FOR DISCLOSURE: 
We make changes to certain geographic identifiers (county, czone) 
*/


sort fips

keep fips czone
order fips czone

save ${ds_czone}, replace
saveold ${ds_czone}_v12, replace v(12)
export delimited ${ds_czone}.csv, replace

/* Attach MSA string: Updated by Adarsh Kumar
Goal is to add the MSA1983 name to corresponding czone (since czone names are 
currently unavailable)
*/
merge 1:1 fips using "${dir_cw}/cw_geo_fips_msa1983.dta", nogen keep(1 3)
keep czone msa1983_str 
duplicates drop
bys czone: gen ct = _n
tab ct
reshape wide msa1983_str, i(czone) j(ct)
replace msa1983_str1 = msa1983_str1 + "; " if msa1983_str2 != ""
replace msa1983_str2 = msa1983_str2 + "; " if msa1983_str3 != ""
replace msa1983_str3 = msa1983_str3 + "; " if msa1983_str4 != ""
replace msa1983_str4 = msa1983_str4 + "; " if msa1983_str5 != ""
replace msa1983_str5 = msa1983_str5 + "; " if msa1983_str6 != ""

gen msa1983_str = msa1983_str1 + msa1983_str2 + msa1983_str3 + msa1983_str4 + msa1983_str5 + msa1983_str6
keep czone msa1983_str
saveold ${ds_czone}_msa1983str_v12, replace v(12)
export delimited ${ds_czone}_msa1983str.csv, replace


di "Ended at $S_DATE $S_TIME"
// End of do file
